<?php
/**
 * Created by : PhpStorm
 * User: ZPF
 * Date: 2022/5/12
 * Time: 22:27
 */

namespace app\admin\services;

use app\admin\model\SkuCategoryValueModel;
use app\admin\model\SkuMoldModel;
use think\facade\Validate;

class SkuMoldService extends BaseService
{
    public static function recursionList($p = [])
    {
        $params = [
            'where' => [],
            'page' => 'all',
            'order' => 'level desc'
        ];
        if (!empty($p['name']))
            $params['where'][] = ['name', 'like', '%' . $p['name'] . '%'];
        $list = SkuMoldModel::getList($params)->toArray();
        $list = array_column($list, null, 'id');
        foreach ($list as $key => $item) {
            $list[$key]['byname']=$item['code'].'-'.$item['name'];
            if ($item['parent']) {
                $list[$item['parent']]['children'][] = $list[$key];
                unset($list[$key]);
            }
        }
        api_return(200, '', ['list' => array_values($list)]);
    }

    public static function tableList($p)
    {

        $params = [
            'where' => [],
            'order' => 'id desc',
            'page'=>($p['pageNo'])??1,
            'pageSize'=>($p['pageSize'])??10,
            'field'=>'*'

        ];
        if (!empty($p['keyword']))
            $params['where'][] = ['name|code', 'like', '%'.$p['keyword'].'%'];
        $list = SkuMoldModel::getList($params);
        foreach ($list as $item){
//            $item['id']=(string)$item['id'];
//            $item['object']=json_encode($item['object']);
        }
        $count = SkuMoldModel::count($params['where']);
        api_return(200, '', ['list' => $list, 'total' => $count]);
    }

    public static function save($p)
    {
        $rules = [
            'name' => 'require',
            'code' => 'require',
//            'attribute_name'=>'require',
//            'attribute_spec'=>'require'
        ];
        $validate = Validate::rule($rules);
        if (!$validate->check($p))
            api_return(500, $validate->getError());
//        字段重复检测
//        SkuMoldModel::duplicateCheck('name', $p);

//        if ($p['parent']) {
//            $res = SkuMoldModel::find($p['parent']);
//            if (!$res)
//                api_return(500, 'parent不存在');
//            $parent = $res->toArray();
//            $parent['tree'][] = (string)$p['parent'];
//            $p['tree'] = $parent['tree'];
//        } else {
//            $p['tree'] = [];
//        }
        SkuMoldModel::edit($p);

    }

    public static function del($p)
    {
        $rule = ['ids' => 'require'];
        $validate = \think\facade\Validate::rule($rule);
        if (!$validate->check($p))
            api_return(500, $validate->getError());
        SkuMoldModel::delByIds($p['ids']);

    }

    public static function detial($p)
    {
        $rule = [
            'id' => 'require'
        ];
        $validate = Validate::rule($rule);
        if (!$validate->check($p)) {
            api_return(500, $validate->getError());
        }
        if (!empty($p['production_attribute']))
            $p['production_attribute'] = [$p['production_attribute'] -1];
        else{
            $p['production_attribute'] =[0,1];
        }
        $info = SkuMoldModel::detail([['id', '=', $p['id']]], 'id,name,attribute_name,attribute_spec,input_type,remark');
        $attrNameList = ($info->attribute_name) ? SkuCategoryValueModel::where([['id', 'in', $info->attribute_name],['production_attribute','in',$p['production_attribute']]])
            ->field('id,name,object,input_type')
            ->orderRaw('field(id,' . implode(',', $info->attribute_name) . ')')
            ->select()
            ->toArray() : [];
        $attrSpecList = ($info->attribute_spec) ?
            SkuCategoryValueModel::where([['id', 'in', $info->attribute_spec],['production_attribute','in',$p['production_attribute']]])
            ->field('id,name,object,input_type')
            ->orderRaw('field(id,' . implode(',', $info->attribute_spec) . ')')
            ->select()
            ->toArray() : [];
        $info['attrNameList'] = $attrNameList;
        $info['attrSpecList'] = $attrSpecList;
        api_return(200, '', $info);
    }
}
